//
//  LoginViewController.m
//  doubanFun
//
//  Created by bing on 16/3/23.
//  Copyright © 2016年 bing. All rights reserved.
//

#import "RESTClient.h"
#import "AppDelegate.h"
#import "LoginViewController.h"

@interface LoginViewController () <UITextFieldDelegate>
@property (weak, nonatomic) IBOutlet UITextField *username;
@property (weak, nonatomic) IBOutlet UITextField *password;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    self.password.delegate = self;
    self.password.secureTextEntry = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(closeKey:)];
    [self.view addGestureRecognizer:tap];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)loginAction:(id)sender {
    if (![self checkInputValue]) {
        return;
    }
    NSDictionary *parameters = @{@"app_name":@"radio_android",
                                 @"version":@100,
                                 @"email":_username.text,
                                 @"password":_password.text
                                 };
    NSString *url = [NSString stringWithUTF8String:API_LOGIN];
    [UIHelper showLoadingHUD];
    [_RESTClient method:POST urlString:url parameters:parameters success:^(id responseObject) {
        [UIHelper hideHUD];
        if ([responseObject isKindOfClass:[NSDictionary class]]) {
            DFUser *user = [DFUser initWithDictionary:responseObject];
            NSData *data = [NSKeyedArchiver archivedDataWithRootObject:user];
            [[NSUserDefaults standardUserDefaults] setObject:data forKey:@"user"];
            [[NSUserDefaults standardUserDefaults] synchronize];
            _AppDelegate.user = user;
            [UIHelper showTextHUD:@"登录成功!!!"];
            [[NSNotificationCenter defaultCenter] postNotificationName:USER_LOGIN_SUCCESS object:nil];
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self dismissViewControllerAnimated:YES completion:nil];
        });
    } failure:^(NSError *error) {
        [UIHelper hideHUD];
        if (error.code == 1)
            [UIHelper showTextHUD:@"邮箱或密码错误!!!"];
        else
            [UIHelper showTextHUD:@"登录失败!!!"];
    }];
    
}

- (BOOL)checkInputValue {
    
    if (_username.text.length < 1 || _password.text.length < 1) {
        [UIHelper showTextHUD:@"输入不能为空"];
        return NO;
    }
    
    return YES;
}

- (IBAction)close:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (void)closeKey:(UITapGestureRecognizer *)refognizer {
    [self.view endEditing:YES];
}

// 回车
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    if ([self checkInputValue]) {
        [self loginAction:nil];
    }
    return YES;
}

@end
